<template>
  <div class="add-channel">
      <div class="container">
        <div class="model-title">{{isEdit?'渠道信息修改':'渠道新增'}}</div>
        <!-- 表单 -->
        <div class="form-box">
           <el-form ref="channelForm" :rules="rules" :model="channelForm" label-width="180px">
              <el-form-item prop="id" v-if="isEdit" label="渠道ID">
                <el-input v-model="channelForm.id" disabled ></el-input>
              </el-form-item>
              <el-form-item prop="name" label="渠道名字">
                <el-input v-model="channelForm.name" ></el-input>
              </el-form-item>
              <el-form-item prop="channelTag" label="渠道标识">
                <el-input v-model="channelForm.channelTag" ></el-input>
              </el-form-item>
              
              <el-form-item prop="autoclearing" label="是否自动清算">
                <el-select class="info-select" v-model="channelForm.autoclearing" placeholder="请选择">
                    <el-option
                    v-for="item in autoclearTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="channelType" label="通道类型">
                <!-- 0 表示 T+0 1表示 T+1 2表示 D+0 3表示 D+1  -->
                <el-select class="info-select" v-model="channelForm.channelType" placeholder="请选择">
                    <el-option
                    v-for="item in channelTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="channelParams" label="渠道参数">
                <el-input v-model="channelForm.channelParams" ></el-input>
              </el-form-item>
              <el-form-item prop="channelNo" label="渠道商家标号">
                <el-input v-model="channelForm.channelNo" ></el-input>
              </el-form-item>
              <el-form-item prop="notifyURL" label="异部回调url">
                <el-input v-model="channelForm.notifyURL" ></el-input>
              </el-form-item>
              <el-form-item prop="returnURL" label="同步回调的url">
                <el-input v-model="channelForm.returnURL" ></el-input>
              </el-form-item>
              <el-form-item prop="subChannelTag" label="子渠道标识">
                <el-input v-model="channelForm.subChannelTag" ></el-input>
              </el-form-item>
              <el-form-item prop="subName" label="子渠道名字">
                <el-input v-model="channelForm.subName" ></el-input>
              </el-form-item>
              <el-form-item prop="singleMinLimit" label="单次最低限额">
                <el-input v-model="channelForm.singleMinLimit" ></el-input>
              </el-form-item>
              <el-form-item prop="singleMaxLimit" label="单次最高额度">
                <el-input v-model="channelForm.singleMaxLimit" ></el-input>
              </el-form-item>
              <el-form-item prop="everyDayMaxLimit" label="单日最高额度">
                <el-input v-model="channelForm.everyDayMaxLimit" ></el-input>
              </el-form-item>
              <el-form-item prop="minwithdrawFee" label="成本提现费">
                <el-input v-model="channelForm.minwithdrawFee" ></el-input>
              </el-form-item>
              <el-form-item prop="minextrafee" label="成本额外费用">
                <el-input v-model="channelForm.minextrafee" ></el-input>
              </el-form-item>
              <el-form-item prop="costRate" label="成本费率">
                <el-input v-model="channelForm.costRate" ></el-input>
              </el-form-item>
              <el-form-item prop="startTime" label="通道开始可以使用的时间">
                <el-time-picker
                  v-model="channelForm.startTime"
                  :value-format="'HH:mm:ss'"
                  placeholder="通道结算的时间">
                </el-time-picker>
              </el-form-item>
              <el-form-item prop="endTime" label="通道结算的时间">
                <el-time-picker
                  v-model="channelForm.endTime"
                  :value-format="'HH:mm:ss'"
                  placeholder="通道结算的时间">
                </el-time-picker>
              </el-form-item>
              
              <el-form-item prop="status" label="通道状态">
                <!-- 0:关闭；1：正常使用；2：试运营 -->
                <el-select class="info-select" v-model="channelForm.status" placeholder="请选择">
                    <el-option
                    v-for="item in channelStatusList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="log" label="渠道log">
                <el-input v-model="channelForm.log" ></el-input>
              </el-form-item>
              <el-form-item prop="costRate" label="成本费率">
                <el-input v-model="channelForm.costRate" ></el-input>
              </el-form-item>
              <el-form-item prop="remarks" label="通道备注">
                <el-input v-model="channelForm.remarks" ></el-input>
              </el-form-item>
           </el-form>
           <div class="submit-box">
             <el-button type="danger" @click="handleSubmit" size="medium" style="width:200px">提交</el-button>
         </div>
        </div>
      </div>
  </div>
</template>

<script>
export default {
  name:'add-channel',
  data () {
    return {
        isEdit:false,
        channelTypeList:[
          {value:0,label:'T+0'},
          {value:1,label:'T+1'},
          {value:2,label:'D+0'},
          {value:3,label:'D+1'},
        ],
        channelStatusList:[
          {value:0,label:'关闭'},
          {value:1,label:'正常使用'},
          {value:2,label:'试运营'},
        ],
        autoclearTypeList:[
          {value:0,label:'否'},
          {value:1,label:'是'},
        ],
        channelForm:{
          "channelTag": "",
          "remarks": "",
          "extendedField": "",
          "channelParams": "",
          "channelNo": "",
          "notifyURL": "",
          "returnURL": "",
          "autoclearing": 0,
          "channelType": 0,
          "name": "",
          "subChannelTag": "",
          "subName": "",
          "singleMinLimit": '',
          "singleMaxLimit": '',
          "everyDayMaxLimit": '',
          "minwithdrawFee": '',
          "minextrafee": '',
          "startTime": "00:00:00",
          "endTime": "23:59:59",
          "costRate": "",
          "status": 1,
          "log": "",
          "coinType": ""
        },
        rules:{
          channelTag:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          remarks:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          channelParams:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          channelNo:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          autoclearing:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          channelType:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          name:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          subChannelTag:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          subName:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          singleMinLimit:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          singleMaxLimit:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          minwithdrawFee:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          minextrafee:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          startTime:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          endTime:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          costRate:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          status:[
            {required: true, message: '此项为必填项', trigger: 'blur' }
          ],
        }
    };
  },
  components: {},
  created() {
    if(!!this.$route.params.channelInfo){
      this.channelForm = {...this.$route.params.channelInfo,createTime:null};
      this.isEdit = true;
    }else{
      this.isEdit = false;
    }
  },
  mounted() {},
  methods: {
    handleSubmit(){
      this.$confirm('您确认要提交信息吗？','温馨提示',{
        confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(_=>{
        this._submitForm();
      })
    },
    checkInfo() {
      let status = false;
      this.$refs.channelForm.validate(valid => {
        status = valid;
        if (!valid) {
          this.$notify({
            title: "警告",
            message: "填写的信息有误！无法提交！请重新填写！",
            type: "warning"
          });
          return false;
        }
      });
      return status;
    },
     _submitForm(){
       if (!this.checkInfo()) {
        return;
      }
       const token = sessionStorage.getItem(1);
       const url = `user/channel/saveOfUpdateChannelInfo/${token}`
       let params = [];
       if(this.isEdit){
         params.push(`operating=1`);
       }else{
         params.push(`operating=0`);
       }
       for(let key in this.channelForm){
         if(this.channelForm[key]!=="" && this.channelForm[key] !== null){
           params.push(`${key}=${this.channelForm[key]}`);
         }
       }
       params = params.join('&');
        this.$Http.post(url,params).then(res=>{
          if(res.data.resp_code === '000000'){
            this.$message.success('操作成功！');
            this.$router.push({name:'channelManage'})
          }else{
            this.$message(res.data.resp_message);
          }
        }).catch(err=>{
          this.$message.error('服务器繁忙！请稍候再试！')
        })

     }
  }
}

</script>
<style scoped>
.add-channel{
  box-sizing: border-box;
  padding: 20x;
}
.form-box{
  padding: 40px 30px;
  max-width: 600PX;
}
.submit-box{
  padding-left: 200px;
}
</style>